################################################
# Prepare GPS Data
################################################

cat("\nPreparing GPS data.")

library(tidyverse)
library(haven)

gps <- read_dta("GPS/Global Party Survey by Party Stata V2_1_Apr_2020.dta")

# Adjust Position and Salience Variable Names

gps$lrecon_gps <- gps$V4_Scale %>% as.numeric()
gps$galtan_gps <- gps$V6_Scale %>% as.numeric()

gps$lrecon_salience_gps <- gps$V5 %>% as.numeric()
gps$galtan_salience_gps <- gps$V7 %>% as.numeric()

gps$populism_gps <- gps$V8_Scale %>% as.numeric()

# Adjust IDs

gps$id_gps <- gps$ID_GPS %>% as.numeric()
gps$id_partyfacts <- gps$ID_PartyFacts %>% as.numeric()
gps$id_parlgov <- gps$ID_ParlGov %>% as.numeric()
gps$id_ches <- gps$ID_CHES %>% as.numeric()
gps$id_cmp <- gps$ID_CMP %>% as.numeric()
gps$id_vdem <- gps$ID_VDem %>% as.numeric()

# Country and Party Name

gps$country_gps <- as_factor(gps$Country) %>% paste
gps$partyname_gps <- as_factor(gps$Partyname) %>% paste

# Small DF for export

gps_prep <- select(gps,ends_with("_gps", ignore.case = F),
                    starts_with("id_", ignore.case = F)) %>%
  select(country_gps,
         partyname_gps,
         starts_with("id_"),
         everything())

saveRDS(gps_prep, "GPS/gps_prep.rds")

cat("\nGPS data preparation finished.\n")